package com.hazy.animalface.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.hazy.animalface.BaseActivity;
import com.hazy.animalface.R;
import com.hazy.animalface.adapter.DownloadStickersAdapter;
import com.hazy.animalface.module.Sticker;
import com.hazy.animalface.module.StickerBag;
import com.hazy.animalface.presenter.ILoadPresenter;
import com.hazy.animalface.presenter.ILoadPresenterCompl;
import com.hazy.animalface.utils.AnimalUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by pengjiahui on 2017/4/14 0014.
 * <p>
 * 下载界面 主要是下载头贴
 */
public class DownloadActivity extends BaseActivity implements ILoadView, DownloadStickersAdapter.ILoadItemStickerDetail {

    @Bind(R.id.toolbar)
    Toolbar toolbar;                         // 工具栏
    @Bind(R.id.server_stickers_list)
    RecyclerView mServerStickersList;        // 头贴列表

    private ILoadPresenter iLoadPresenter;

    private DownloadStickersAdapter stickersAdapter;        // 头贴包适配器
    private List<StickerBag> stickerBagList;                // 头贴包列表

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_download);

        // 设置工具栏
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        // 显示加载框
        AnimalUtils.showWaitDialog(this);

        iLoadPresenter = new ILoadPresenterCompl(this);
        iLoadPresenter.onLoad(1);

        stickerBagList = new ArrayList<>();
        mServerStickersList.setLayoutManager(new GridLayoutManager(this, 3));
        stickersAdapter = new DownloadStickersAdapter(this, stickerBagList, this);
        mServerStickersList.setAdapter(stickersAdapter);
    }

    @Override
    public void onLoadResult(List<StickerBag> stickerBags) {
        stickerBagList.addAll(stickerBags);
        stickersAdapter.notifyDataSetChanged();
        AnimalUtils.dismissWaitDialog();
    }

    @Override
    public void onLoadSrickerDetail(boolean result, List<Sticker> stickers) {
        AnimalUtils.dismissWaitDialog();
    }

    @Override
    public void onCreateResult(boolean result, String message) {

    }

    @Override
    public void onUploadResult(boolean result, int progress, String message) {

    }

    @Override
    public void onLoadDetail(StickerBag stickerBag) {
        if (stickerBag != null ) {
            StickersActivity.actionStart(this, stickerBag, true);
//            AnimalUtils.showWaitDialog(this);
//            iLoadPresenter.loadSticiersByName(stickerBag.getName());
        }
    }
}
